package popoer.price.thief.service;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;

import popoer.price.thief.domain.Category;
import popoer.price.thief.domain.Product;

public class JingDongThief extends BaseThief implements Thief{
	/**
	 * Logger for this class
	 */
	static final Logger logger = Logger.getLogger(JingDongThief.class.getName());

	String baseURL = "http://www.360buy.com/";
	private String categoryURL = "";
	String encoding = "gbk";
	
	@Override
	public List<Category> getCategories() throws Exception {
		String html = getContent(this.categoryURL);	
		List<Category> categories = new ArrayList<Category>();
//		String regex = "<h2><img.*alt=\"(.+?)\"/></h2>";
		String regex = "<div id=\"_JD_SORTLIST\" class=\"allsort-beta\">.*?<div class=\"extra\">";
		logger.fine("regex:"+regex);
		Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
		
		Matcher matcher = pattern.matcher(html);
		if(matcher.find()){
			String frag = matcher.group();
			logger.fine("frag:"+frag);
			regex = "<h3.*?.com/(.*?)\".*?>(.*?)</a></h3>|<em><a href=\"(.*?)\">(.*?)</a></em>";
			pattern = Pattern.compile(regex);
			matcher = pattern.matcher(frag);
			Category parent = null;
			while(matcher.find()){
				String url1 = matcher.group(1);
				String name1 = matcher.group(2);
				String url2 = matcher.group(3);
				String name2 = matcher.group(4);
				if(StringUtils.isNotEmpty(name1)){
					Category category = new Category();
					category.setLink(url1);
					category.setName(name1);
					categories.add(category);
					parent = category;					
				}else if(StringUtils.isNotEmpty(name2)&&(!name2.startsWith("更多"))){
					Category category = new Category();
					category.setLink(url2);
					category.setName(name2);
					category.setParent(parent);
					parent.addChild(category);
				}
			}
			
		}
		logger.fine("categories size:"+categories.size());
		return categories;
	}
	
	@Override
	public List<Product> getProducts(String relativeURL) throws Exception {
		
		List<Product> products = new ArrayList<Product>();
		while(StringUtils.isNotEmpty(relativeURL) && products.size()<200){
			String html = getContent(relativeURL);		
			String regex = "<div class='p-name'><a target='_blank' href='(.*?)'>(.*?)</a></div>.*?<div class='p-price'><del>￥(.*?)</del><img.*?src=\"(.*?)\">";
			Pattern pattern = Pattern.compile(regex,Pattern.DOTALL);
			Matcher matcher = pattern.matcher(html);
			while (matcher.find()) {
				Product product = new Product(matcher.group(2), NumberUtils.toFloat(matcher.group(3)), matcher.group(1));
				product.setPriceImg(matcher.group(4));
				products.add(product);
			}
			
			regex = "<a href=\"((.(?!href))*?)\" class=\"next\">";
			pattern = Pattern.compile(regex);
			matcher = pattern.matcher(html);
			if(matcher.find()){
				relativeURL = "products/"+matcher.group(1);
			}else{
				relativeURL = null;
			}
			logger.fine("relativeURL:"+relativeURL);
		}
		
		logger.info("products number:"+products.size());
		return products;
	}

	public String getBaseURL() {
		return baseURL;
	}

	public String getEncoding() {
		return encoding;
	}
	
	
}
